<?php
/*************** MVC CONFIGS *******************/
define('VS_NORMAL', 0);
define('VS_PLUGIN', 1);
define('VS_JSON_NORMAL', 2);

define('DELIMITER', '/');
define('LANGUAGE_ENGLISH', 'en');
define('LOCATION_US', 'US');

define('SESSION_USER_OBJECT', 'current_user_dto');

ini_set("SMTP", SMTP_IP);


function incSendMail($email, $template_name, $params)
{
	if (EMAIL_NOTIFICATIONS_ACTIVE) {
		$fullpath = HOME . "mail_templates/" . $template_name . ".eml";
	
		$hFile = fopen($fullpath, 'r');
	
		$from = fgets($hFile);
			
		$headers = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'To: '.$params['firstname'].' '.$params['lastname'].'<'.$email.'>'. "\r\n";
		$headers .= 'From: '.$from."\r\n";
		
		$subject = fgets($hFile);
		$action_cmd = fgets($hFile);
		
		fclose($hFile);
		
		$body = file_get_contents(SITE_URL."?cmd=".$action_cmd."&".$params['querystr']);
	
		foreach($params as $key=>$value)
		{
			$body = str_replace($key, $value, $body);
			$subject = str_replace($key, $value, $subject);
		}
	
		return mail($email, $subject, $body, $headers);
	}
}

function redirect($destination){
	header('Location:'.$destination);
	die();
}

function getHostUrl() {
	$httpHost = $_SERVER["HTTP_HOST"];
	return "http://".$httpHost;
}

function array_obj_diff ($array1, $array2, $keepKeys = false ) {
	foreach ($array1 as $key => $value) {
		$array1[$key] = serialize ($value);
	}

	foreach ($array2 as $key => $value) {
		$array2[$key] = serialize ($value);
	}

	$array_diff = array_diff ($array1, $array2);

	foreach ($array_diff as $key => $value) {
		$array_diff[$key] = unserialize ($value);
	}

	if($keepKeys == false) {
		return array_values($array_diff);
	}
	return $array_diff;
}

function getCurrentWeekRange()
{
	$week = date('W');
	$i = 0;
	while(date('W', strtotime("-$i day")) >= $week) {
		$start_date = date('Y-m-d 00:00:00', strtotime("-$i day"));
		$i++;
	}   
	
	list($yr, $mo, $da) = explode('-', $start_date);   
	$end_date = date('Y-m-d 00:00:00', mktime(0, 0, 0, $mo, $da + 7, $yr));
	
	return array("start" => $start_date, "end" => $end_date);
}

function getLastWeekRange()
{
	$week = date('W')-1;
	$i = 0;
	while(date('W', strtotime("-$i day")) >= $week) {
		$start_date = date('Y-m-d 00:00:00', strtotime("-$i day"));
		$i++;
	}   
	
	list($yr, $mo, $da) = explode('-', $start_date);   
	$end_date = date('Y-m-d 00:00:00', mktime(0, 0, 0, $mo, $da + 7, $yr));
	
	return array("start" => $start_date, "end" => $end_date);
}

function getDefaultRange($period)
{
	$date = explode("#", $period);
	
	$dateStart = explode("-", $date[0]);
	$dateEnd = explode("-", $date[1]);
	
	$start_date = date("Y-m-d 00:00:00", mktime(0, 0, 0, $dateStart[1], $dateStart[2], $dateStart[0]));
	$end_date = date("Y-m-d 23:59:59",  mktime(0, 0, 0, $dateEnd[1], $dateEnd[2], $dateEnd[0]));
	
	return array("start" => $start_date, "end" => $end_date);
}

function getDatesRange($period)
{
	$dateRange = null;
	
	switch ($period)
	{
		case "this_month":
			$start = date("Y-m-d 00:00:00", mktime(0, 0, 0, date("m"), 1, date("Y")));
			$end = date("Y-m-d 00:00:00", mktime(0, 0, 0, date("m")+1, 1, date("Y")));
			$dateRange = array(
				"start" => $start,
				"end" => $end
			);
			break;
		case "last_month":
			$start = date("Y-m-d 00:00:00", mktime(0, 0, 0, date("m")-1, 1, date("Y")));
			$end = date("Y-m-d 00:00:00", mktime(0, 0, 0, date("m"), 1, date("Y")));
			$dateRange = array(
				"start" => $start,
				"end" => $end
			);
			break;
			
		case "this_week":
			$dateRange = getCurrentWeekRange();
			break;
		
		case "last_week":
			$dateRange = getLastWeekRange();
			break;
			
		case "today":
			$start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
			$end = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));
			$dateRange = array(
				"start" => $start,
				"end" => $end
			);
			break;
			
		case "yesterday":
			$start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d")-1, date("Y")));
			$end = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
			$dateRange = array(
				"start" => $start,
				"end" => $end
			);
			break;
		case "all":
			break;	
		default:
			$dateRange = getDefaultRange($period);
	}
	
	return $dateRange;
}

function ago($datefrom,$dateto=-1)
{
	// Defaults and assume if 0 is passed in that
	// its an error rather than the epoch

	if ($datefrom == 0)
	{
		return "A long time ago";
	}
	if ($dateto == -1)
	{
		$time_shift = TIME_OFFSET + DAYLIGHTSAVING;
		$dateto = time() + $time_shift * 3600; 
	}
	
	// Make the entered date into Unix timestamp from MySQL datetime field
	$datefrom = strtotime($datefrom);
	
	// Calculate the difference in seconds betweeen
	// the two timestamps
	
	$difference = $dateto - $datefrom;
	
	// Based on the interval, determine the
	// number of units between the two dates
	// From this point on, you would be hard
	// pushed telling the difference between
	// this function and DateDiff. If the $datediff
	// returned is 1, be sure to return the singular
	// of the unit, e.g. 'day' rather 'days'
	switch(true)
	{
		// If difference is less than 60 seconds,
		// seconds is a good interval of choice
		case(strtotime('-1 min', $dateto) < $datefrom):
			$datediff = $difference;
			$res = ($datediff==1) ? $datediff.' second ago' : $datediff.' seconds ago';
			break;
		// If difference is between 60 seconds and
		// 60 minutes, minutes is a good interval
		case(strtotime('-1 hour', $dateto) < $datefrom):
			$datediff = floor($difference / 60);
			$res = ($datediff==1) ? $datediff.' minute ago' : $datediff.' minutes ago';
			break;
		// If difference is between 1 hour and 24 hours
		// hours is a good interval
		case(strtotime('-1 day', $dateto) < $datefrom):
			$datediff = floor($difference / 60 / 60);
			$res = ($datediff==1) ? $datediff.' hour ago' : $datediff.' hours ago';
			break;
		// If difference is between 1 day and 7 days
		// days is a good interval               
		case(strtotime('-1 week', $dateto) < $datefrom):
			$day_difference = 1;
			while (strtotime('-'.$day_difference.' day', $dateto) >= $datefrom)
			{
				$day_difference++;
			}
		   
			$datediff = $day_difference;
			$res = ($datediff==1) ? 'yesterday' : $datediff.' days ago';
			break;
		// If difference is between 1 week and 30 days
		// weeks is a good interval           
		case(strtotime('-1 month', $dateto) < $datefrom):
			$week_difference = 1;
			while (strtotime('-'.$week_difference.' week', $dateto) >= $datefrom)
			{
				$week_difference++;
			}
		   
			$datediff = $week_difference;
			$res = ($datediff==1) ? 'last week' : $datediff.' weeks ago';
			break;           
		// If difference is between 30 days and 365 days
		// months is a good interval, again, the same thing
		// applies, if the 29th February happens to exist
		// between your 2 dates, the function will return
		// the 'incorrect' value for a day
		case(strtotime('-1 year', $dateto) < $datefrom):
			$months_difference = 1;
			while (strtotime('-'.$months_difference.' month', $dateto) >= $datefrom)
			{
				$months_difference++;
			}
		   
			$datediff = $months_difference;
			$res = ($datediff==1) ? $datediff.' month ago' : $datediff.' months ago';

			break;
		// If difference is greater than or equal to 365
		// days, return year. This will be incorrect if
		// for example, you call the function on the 28th April
		// 2008 passing in 29th April 2007. It will return
		// 1 year ago when in actual fact (yawn!) not quite
		// a year has gone by
		case(strtotime('-1 year', $dateto) >= $datefrom):
			$year_difference = 1;
			while (strtotime('-'.$year_difference.' year', $dateto) >= $datefrom)
			{
				$year_difference++;
			}
		   
			$datediff = $year_difference;
			$res = ($datediff==1) ? $datediff.' year ago' : $datediff.' years ago';
			break;
		   
	}
	return $res;
}

function timeLeft($endDate)
{
	$endDate = strtotime($endDate);
	
	$time_shift = TIME_OFFSET + DAYLIGHTSAVING;
	$nowDate = time();
	
	$leftDate = $endDate - $nowDate;
	
	$leftDays = floor($leftDate / 60 / 60 / 24);
	$leftHours = $leftDate / 60 / 60 % 24;
	$leftMins = $leftDate / 60 % 60;
	$leftSecs = $leftDate % 60;
	
	switch (true)
	{
		case ($leftDays == 0):
			$todayEndTime = date("H:i", $endDate);
			$res = "Today at ".$todayEndTime;
			break;
			
		case ($leftDays == 1):
			$res = "Tomorrow";
			break;
			
		case ($leftDays > 1):
			$res = $leftDays . " days left";
			break;
			
		default:
			$res = "";
	}
	
	return $res;
}

?>
